package com.hsy.linemoney.apiservice;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.requests.RequestTask;
import com.hsy.linemoney.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 注册相关
 * Created by MartinKent on 2016/11/23.
 */

public class RegistrationService extends BasicService<JSONArray> {
    //    邮箱是否已验证
    public void isEmailAddressVerified(String email, final Callback<IsEmailAddressVerifiedResult> callback) {
        String api = "/LmRegistration-portlet.account/isEmailAddressVerified";
        JSONObject params = ParamsMaker.newInstance().add("emailAddress", email).get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                if (null != callback) {
                    try {
                        callback.onSuccess(0, DPGsonUtil.fromJson(result.get(0).toString(), IsEmailAddressVerifiedResult.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }
        });
    }

    public static class IsEmailAddressVerifiedResult {
        private Status status;

        private Value value;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setValue(Value value) {
            this.value = value;
        }

        public Value getValue() {
            return this.value;
        }

        public static class Status {
            private String message;

            private int code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(int code) {
                this.code = code;
            }

            public int getCode() {
                return this.code;
            }

        }

        public static class Value {
            private boolean isEmailAddressVerified;

            public void setIsEmailAddressVerified(boolean isEmailAddressVerified) {
                this.isEmailAddressVerified = isEmailAddressVerified;
            }

            public boolean getIsEmailAddressVerified() {
                return this.isEmailAddressVerified;
            }
        }
    }

    //向用户邮箱发送验证码
    public void sendEmailAddressVerification(String email, final Callback<SendEmailAddressVerificationResult> callback) {
        String api = "/LmRegistration-portlet.account/sendEmailAddressVerification";
        JSONObject params = ParamsMaker.newInstance().add("emailAddress", email).get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                if (null != callback) {
                    try {
                        callback.onSuccess(0, DPGsonUtil.fromJson(result.get(0).toString(), SendEmailAddressVerificationResult.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }
        });
    }

    public static class SendEmailAddressVerificationResult {
        private Status status;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public static class Status {
            private String message;

            private int code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(int code) {
                this.code = code;
            }

            public int getCode() {
                return this.code;
            }

        }
    }

    //发送新密码（忘记密码）
    public void sendPassword(String email, final Callback<SendPasswordResult> callback) {
        String api = "/LmRegistration-portlet.account/sendPassword";
        JSONObject params = ParamsMaker.newInstance().add("emailAddress", email).get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                if (null != callback) {
                    try {
                        callback.onSuccess(0, DPGsonUtil.fromJson(result.get(0).toString(), SendPasswordResult.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }
        });
    }

    public static class SendPasswordResult {
        private Status status;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public static class Status {
            private String message;

            private int code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(int code) {
                this.code = code;
            }

            public int getCode() {
                return this.code;
            }

        }
    }

    //    设置同意用户协议
    public void setAgreedToTermsOfUse(String email, final Callback<SetAgreedToTermsOfUseResult> callback) {
        String api = "/LmRegistration-portlet.account/updateAgreedToTermsOfUse";
        JSONObject params = ParamsMaker.newInstance()
                .add("companyId", 20155)
                .add("emailAddress", email)
                .add("agreedToTermsOfUse", true)
                .get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                if (null != callback) {
                    try {
                        callback.onSuccess(0, DPGsonUtil.fromJson(result.get(0).toString(), SetAgreedToTermsOfUseResult.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }
        });
    }

    public static class SetAgreedToTermsOfUseResult {
        public class Status {
            private String message;

            private int code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(int code) {
                this.code = code;
            }

            public int getCode() {
                return this.code;
            }

        }

        public class Value {
            private boolean isTermsOfUseAgreed;

            public void setIsTermsOfUseAgreed(boolean isTermsOfUseAgreed) {
                this.isTermsOfUseAgreed = isTermsOfUseAgreed;
            }

            public boolean getIsTermsOfUseAgreed() {
                return this.isTermsOfUseAgreed;
            }

        }

        private Status status;

        private Value value;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setValue(Value value) {
            this.value = value;
        }

        public Value getValue() {
            return this.value;
        }

    }

    //    验证邮箱
    public void verifyEmailAddressWithKey(String ticketKey, final Callback<VerifyEmailAddressWithKeyResult> callback) {
        String api = "/LmReferenceServices-portlet.status/verify-email-address-with-key";
        JSONObject params = ParamsMaker.newInstance().add("ticketKey", ticketKey).get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                if (null != callback) {
                    try {
                        callback.onSuccess(0, DPGsonUtil.fromJson(result.get(0).toString(), VerifyEmailAddressWithKeyResult.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }
        });
    }

    public static class VerifyEmailAddressWithKeyResult {
        private Status status;

        private Value value;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setValue(Value value) {
            this.value = value;
        }

        public Value getValue() {
            return this.value;
        }

        public static class Status {
            private String message;

            private String code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(String code) {
                this.code = code;
            }

            public String getCode() {
                return this.code;
            }

        }

        public static class Value {

        }
    }

    //    更新邮箱
    public void updateEmailAddress(String emailAddress, final Callback<UpdateEmailAddressResult> callback) {
        String api = "/user/update-email-address";
        JSONObject params = ParamsMaker.newInstance().add("emailAddress1", emailAddress).add("emailAddress2", emailAddress).add("password", LineMoneyContext.getPassword()).get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                if (null != callback) {
                    try {
                        callback.onSuccess(0, DPGsonUtil.fromJson(result.get(0).toString(), UpdateEmailAddressResult.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
//                if (e instanceof RequestTask.StatusCodeException) {
//                    ToastUtil.showToast(msg);
//                } else {
//                    ToastUtil.showToast(R.string.txt_network_failure);
//                }
                if (null != callback) {
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }
        }, false);
    }

    public static class UpdateEmailAddressResult {

    }
}
